import axios from 'axios';
import {
  responseSuccessInterceptor,
  responseErrorInterceptor,
} from './../interceptors/response';
import defaultSetting from '../config';

export default function Api({
  prefix = '',
  host,
}: {
  prefix?: string;
  host?: string;
}) {
  return function <T extends { new (...args: any[]) }>(constructor: T) {
    const hostUrl = host === undefined ? defaultSetting.baseURL : '';
    const axiosInstance = axios.create({
      ...defaultSetting,
      baseURL: hostUrl + prefix,
    });
    axiosInstance.interceptors.response.use(
      responseSuccessInterceptor,
      responseErrorInterceptor,
    );
    return class extends constructor {
      http = axiosInstance;
    };
  };
}
